﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.ComponentModel;
using XCI.Sys.Service;
using XCI.Windows.Controls;
using XCI.Sys.Model;
using XCI.Windows;
using System.Collections.Generic;
using System;
using DevExpress.XtraEditors.Repository;
using System.Drawing;
using DevExpress.XtraEditors.Registrator;

namespace XCI.Sys.Controls
{
    /// <summary>
    /// 数据字典下拉控件
    /// </summary>
    public class DicComboBoxEdit : ComboBoxEditx
    {
        static DicComboBoxEdit()
        {
            RepositoryItemDicComboBoxEdit.RegisterDicComboBoxEdit();
        }

        public DicComboBoxEdit()
        {
        }


        private AsyncWorker asyncWorker;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string ValueMember => "Value";
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string DisplayMember => "Name";

        /// <summary>
        /// 是否显示全部项
        /// </summary>
        [DefaultValue(true)]
        [Category("XCI")]
        [Description("是否显示全部项")]
        public virtual bool AllItemVisible { get; set; } = true;

        /// <summary>
        /// 全部项文本
        /// </summary>
        [DefaultValue("全部分类")]
        [Category("XCI")]
        [Description("全部项文本")]
        public virtual string AllItemCaption { get; set; } = "全部";

        /// <summary>
        /// 字典编码
        /// </summary>
        [Category("XCI")]
        [Description("字典编码")]
        [DefaultValue(null)]
        public string DicCode { get; set; }

        /// <summary>
        /// 异步数据加载完成事件
        /// </summary>
        [Category("XCI"), Description("异步数据加载完成事件")]
        public event EventHandler<EventArgs> DataLoadComplet;

        /// <summary>
        /// 是否异步加载
        /// </summary>
        [Category("XCI")]
        [Description("是否异步加载")]
        [DefaultValue(true)]
        public bool AsyncLoad { get; set; } = true;

        protected override void OnLoaded()
        {
            base.OnLoaded();
            if (DesignMode) return;
            if (string.IsNullOrEmpty(DicCode)) return;

            List<SysDic> list = null;
            if (AsyncLoad)
            {
                if (asyncWorker == null) asyncWorker = new AsyncWorker();
                asyncWorker.RunCallback(e => list = DicService.Instance.SelectListByCode(DicCode))
                .CompletCallback(e => BindData(list))
                .RunAsync();
            }
            else
            {
                list = DicService.Instance.SelectListByCode(DicCode);
                BindData(list);
            }
        }

        private void BindData(List<SysDic> list)
        {
            if (AllItemVisible)
            {
                list.Insert(0, new SysDic
                {
                    Id = string.Empty,
                    Name = AllItemCaption
                });
            }
            DataSource = list;
            DataLoadComplet?.Invoke(this, EventArgs.Empty);
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemDicComboBoxEdit Properties => base.Properties as RepositoryItemDicComboBoxEdit;

        public override string EditorTypeName => RepositoryItemDicComboBoxEdit.CustomEditName;
    }

    [UserRepositoryItem("RegisterDicComboBoxEdit")]
    public class RepositoryItemDicComboBoxEdit : RepositoryItemComboBoxEditx
    {
        static RepositoryItemDicComboBoxEdit()
        {
            RegisterDicComboBoxEdit();
        }

        public new const string CustomEditName = "DicComboBoxEdit";

        public RepositoryItemDicComboBoxEdit()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterDicComboBoxEdit()
        {
            Image img = null;
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName,
                typeof(DicComboBoxEdit), typeof(RepositoryItemDicComboBoxEdit),
                typeof(ComboBoxEditxViewInfo), new ComboBoxEditxPainter(), true, img));
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                RepositoryItemDicComboBoxEdit source = item as RepositoryItemDicComboBoxEdit;
                if (source == null) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }
    }
}